
package kaizone.haining.chaobohui.widget;

import java.util.List;

import kaizone.haining.chaobohui.R;
import kaizone.haining.chaobohui.json.NewsItem;
import kaizone.haining.chaobohui.json.Item.PicModel;
import kaizone.haining.chaobohui.util.ImageFetcher;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CBHItemView extends LinearLayout {

    private ImageView mImageView;

    private TextView mTitleTextView;

    private TextView mDescriptiontTextView;

    private TextView mTopicTextView;

    private ImageFetcher mImageFetcher;

    public CBHItemView(Context context) {
        super(context);
    }

    public CBHItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {

    }

    public void setImageFetcher(ImageFetcher imageFetcher) {
        mImageFetcher = imageFetcher;
    }

    public void setTitle(String text) {
        mTitleTextView.setText(text);
    }

    public void loadImage(String url, ImageFetcher imageFetcher) {
        imageFetcher.loadImage(url, mImageView);
    }

    public void setDescription(String text) {
        mDescriptiontTextView.setText(text);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mImageView = (ImageView) findViewById(R.id.img);
        mTitleTextView = (TextView) findViewById(R.id.tv01);
        mDescriptiontTextView = (TextView) findViewById(R.id.tv02);
        mTopicTextView = (TextView) findViewById(R.id.tv03);
    }

    public void setImageLayoutParams(int width, int height) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
        mImageView.setLayoutParams(params);
    }

    public void setData(Object obj) {
        if (obj != null && obj instanceof NewsItem) {
            NewsItem newsItem = (NewsItem) obj;
            mTitleTextView.setText(newsItem.getTitle());
            mDescriptiontTextView.setText(newsItem.getSummary());
            String isTop = newsItem.getIsTop();
            if (!TextUtils.isEmpty(isTop)) {
                if (isTop.equals(NewsItem.FLAG_ISTOP_N)) {
                    mTopicTextView.setVisibility(View.GONE);
                } else {
                    // mTopicTextView.setVisibility(View.VISIBLE); test
                    mTopicTextView.setVisibility(View.GONE);
                }
            }

            List<PicModel> picmodels = newsItem.getPicModelList();
            if (picmodels != null && !picmodels.isEmpty()) {
                mImageView.setVisibility(View.VISIBLE);
                PicModel picmodel = picmodels.get(0);
                if (picmodel != null) {
                    String x100 = picmodel.sum100x000;
                    mImageFetcher.loadImage(x100, mImageView);
                } else {
                    mImageView.setVisibility(View.GONE);
                }
            } else {
                mImageView.setVisibility(View.GONE);
            }
        }
    }

}
